package com.ht.medicaltreatment.activity

import android.content.Intent
import android.graphics.Color
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import kotlinx.android.synthetic.main.activity_centrifuge_add_sample.btnStart
import kotlinx.android.synthetic.main.activity_centrifuge_add_sample.icon
import kotlinx.android.synthetic.main.activity_centrifuge_add_sample.imgBg
import kotlinx.android.synthetic.main.activity_centrifuge_add_sample.imgBlueBg
import kotlinx.android.synthetic.main.activity_centrifuge_add_sample.tvQueryAdd
import kotlinx.android.synthetic.main.activity_centrifuge_add_sample.tvScanAdd
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.launch

class CentrifugeAddSampleActivity : BaseActivity(), View.OnClickListener {

    override fun getLayoutId() = R.layout.activity_centrifuge_add_sample

    private var centrifugeId = 0
    private var isLeft = true
    private var moveX = 0

    override fun init() {
        tvScanAdd.setOnClickListener(this)
        tvQueryAdd.setOnClickListener(this)
        btnStart.setOnClickListener(this)
        centrifugeId = intent.getIntExtra("centrifugeId",0)
    }

    override fun onClick(v: View?) {
        when(v){
            tvScanAdd -> move(true)
            tvQueryAdd -> move(false)
            btnStart -> {
                intent = Intent()
                if(isLeft){
                    //扫描添加
                    intent.setClass(this,SampleSearchScanActivity::class.java)
                }else{
                    //查询添加
                    intent.setClass(this,SampleSearchAddActivity::class.java)
                }
                intent.putExtra("centrifugeId",centrifugeId)
                intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
                startActivity(intent)
                finish()
            }
        }
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        moveX = imgBg.right - imgBlueBg.right
    }

    fun move(left:Boolean){
        isLeft = left
        if(!left){
            lifecycleScope.launch {
                flow{
                    (0..moveX step 9).forEach {
                        delay(1)
                        emit(it)
                    }
                }.onCompletion {
                    tvQueryAdd.setTextColor(Color.WHITE)
                    tvScanAdd.setTextColor(resources.getColor(R.color.color66))
                    btnStart.text = "开始查询"

                    icon.setBackgroundResource(R.drawable.icon_scan_chunru)
                }.collect {
                    imgBlueBg.translationX = it.toFloat()
                }
            }
        }else{
            lifecycleScope.launch {
                flow{
                    (moveX downTo 0 step 9).forEach {
                        delay(1)
                        emit(it)
                    }
                }.onCompletion {
                    tvScanAdd.setTextColor(Color.WHITE)
                    tvQueryAdd.setTextColor(resources.getColor(R.color.color66))
                    btnStart.text = "开始识别"
                    icon.setBackgroundResource(R.drawable.icon_chaxun_chunru)
                }.collect {
                    imgBlueBg.translationX = it.toFloat()
                }
            }
        }
    }
}